Release 10.1A: OpenEdge Development:
Programming Interfaces
Using the IMPORT statement
The
IMPORTstatement is the counterpart of theEXPORTstatement. It reads an input file into Progress procedures, one line at a time.Using IMPORT to read standard data
The following example shows
IMPORTreading the file exported by the procedurei-export.p:
This relies on the input being space separated. You can also use the
DELIMITERoption of theIMPORTstatement to read a file with a different separator.For example,
i-imprt2.preads the file produced byi-exprt2.pin the previous section:
This example reads one line at a time from
i-datfl7.dinto the character-string variable data. It then breaks the line into discrete values and assigns them to the fields of a customer record.Using IMPORT to read nonstandard data
Although the
IMPORTstatement is used primarily to read data in the standard format written by theEXPORTstatement. However, you can use theUNFORMATTEDandDELIMITERoptions ofIMPORTto read data in non-standard formats.When you use the
UNFORMATTEDoption, theIMPORTstatement reads one line from the input file. For example, suppose your input file is formatted as follows:
The lines containing cust–num and sales–rep values can be read with normal
IMPORTstatements. However, if you try to read the customer name values with a normalIMPORTstatement, only the first word of each name is read—the space character is treated as a delimiter. To prevent this, read the name with theUNFORMATTEDoption, as ini-impun1.p:
Now, suppose each line of the file contained a cust–num, name, and sales–rep value, but no special delimiters are used. Instead, the fields are defined by their position within the line:
In
i-datfl3.d, the first three character positions in each line are reserved for the cust-num value, the next 17 positions for the name value, and the last three for the sales-rep value. Space characters may occur between fields, but they may also occur within a field value. To process this file with theIMPORTstatement, use theUNFORMATTEDoption to read one line at a time, as shown ini-impun2.p:
After
Note: If a line in your input file ends with a tilde (~), Progress interprets that as a continuation character. This means, that line and the following line are treated as a single line. Therefore, thei-impun2.preads each line, it uses theSUBSTRINGfunction to break the line into field values. It then assigns these values to the appropriate fields in the customer record.IMPORTstatement with theUNFORMATTEDoption reads both lines into a single variable.What if fields values are separated by a delimiter other than the space character? For example, in
i-datfl4.d, field values are separated by commas:
You could use the
UNFORMATTEDoption of theIMPORTstatement to read this file one line at a time and then use theINDEXfunction to locate the commas and break the line into field values. Another solution is to use theDELIMITERoption of theIMPORTstatement as shown ini-impun3.p:
In this example, the
Note: You can only specify a single character as a delimiter. If the value you give with theDELIMITERoption specifies that field values are separated by commas rather than by spaces. Therefore, theIMPORTstatement parses each line correctly and assigns each value to the appropriate field.DELIMITERoption is longer than one character, then only the first character is used.For more information on the
IMPORTstatement, see the OpenEdge Development: Progress 4GL Reference .
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |